﻿Imports Gestion_Licitaciones.localhost
Imports System.Data
Imports System.Data.SqlClient
Public Class Form3
    Sub Carga_Proveedores_licitacion()
        Dim SQL As String = "select * from Licitacion_Proveedor where Cod_Licitacion='" & Licitacion & "'" & " ORDER BY Cod_Proveedor ASC"
        Dim da As SqlDataAdapter
        Dim dt As New DataTable
        Try
            da = New SqlDataAdapter(SQL, nCnn)
            da.Fill(dt)
            If dt.Rows.Count <> 0 Then
                ListView2.Items.Clear()
                For i = 0 To dt.Rows.Count - 1
                    Dim Itm = ListView2.Items.Add("")
                    Itm.SubItems.Add(RTrim(dt.Rows(i)("Cod_Proveedor")))
                    Itm.SubItems.Add(RTrim(dt.Rows(i)("Razon_Social")))
                    Itm.SubItems.Add(RTrim(dt.Rows(i)("Ruc")))
                Next
                ListView2.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
            End If
        Catch ex As Exception
            MsgBox("Importando proveedores licitación: " & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Label2.Text = Licitacion
        Dim objWebService As New localhost.ProveedoresWSClient
        DataGridView1.DataSource = objWebService.listar(Result:="")
        For i = 0 To DataGridView1.RowCount - 1
            Dim Itm = ListView1.Items.Add("")
            Itm.SubItems.Add(RTrim(DataGridView1.Rows(i).Cells(0).Value))
            Itm.SubItems.Add(RTrim(DataGridView1.Rows(i).Cells(1).Value))
            Itm.SubItems.Add(RTrim(DataGridView1.Rows(i).Cells(2).Value))
        Next
        ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
        Call Carga_Proveedores_licitacion()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For i = 0 To ListView1.Items.Count - 1
            If ListView1.Items(i).Checked = True Then
                Dim Itm = ListView2.Items.Add("")
                Itm.SubItems.Add(ListView1.Items(i).SubItems(1).Text)
                Itm.SubItems.Add(ListView1.Items(i).SubItems(2).Text)
                Itm.SubItems.Add(ListView1.Items(i).SubItems(3).Text)
            End If
        Next
        ListView2.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
    End Sub

    Private Sub ListView1_ItemChecked(sender As Object, e As ItemCheckedEventArgs) Handles ListView1.ItemChecked
        For i = 0 To ListView1.Items.Count - 1
            If ListView1.Items(i).Checked = True Then
                ListView1.Items(i).SubItems(0).BackColor = Color.Blue
                ListView1.Items(i).SubItems(0).ForeColor = Color.White
            Else
                ListView1.Items(i).SubItems(0).BackColor = Color.White
                ListView1.Items(i).SubItems(0).ForeColor = Color.Black
            End If
        Next
    End Sub

    Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged

    End Sub

    Private Sub ListView2_ItemChecked(sender As Object, e As ItemCheckedEventArgs) Handles ListView2.ItemChecked
        For i = 0 To ListView2.Items.Count - 1
            If ListView2.Items(i).Checked = True Then
                ListView2.Items(i).SubItems(0).BackColor = Color.Blue
                ListView2.Items(i).SubItems(0).ForeColor = Color.White
            Else
                ListView2.Items(i).SubItems(0).BackColor = Color.White
                ListView2.Items(i).SubItems(0).ForeColor = Color.Black
            End If
        Next
    End Sub

    Private Sub ListView2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView2.SelectedIndexChanged

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        For i = 0 To ListView2.Items.Count - 1
            If ListView2.Items(i).Checked = True Then
                ListView2.Items(i).Remove()
                Exit For
            End If
        Next
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim resp = MsgBox("¿ Desea asiganar a los proveedores ?", MsgBoxStyle.YesNo + MsgBoxStyle.Question)
        If resp = vbYes Then
            Dim SQL As String = "delete from Licitacion_proveedor WHERE Cod_licitacion='" & Licitacion & "'"
            Conn.Open()
            Try
                Dim cdo As New SqlCommand(SQL, Conn)
                cdo.ExecuteNonQuery()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical)
            End Try
            Conn.Close()

            For i = 0 To ListView2.Items.Count - 1
                Dim SQL1 As String = "INSERT INTO Licitacion_proveedor(Cod_licitacion, Cod_proveedor, Razon_Social, Ruc, Cod_estado)" & _
                                 " VALUES('" & Licitacion & "', '" & ListView2.Items(i).SubItems(1).Text & "', '" & ListView2.Items(i).SubItems(2).Text & "', '" & ListView2.Items(i).SubItems(3).Text & "', '06')"
                Conn.Open()
                Try
                    Dim cdo As New SqlCommand(SQL1, Conn)
                    cdo.ExecuteNonQuery()

                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Critical)
                End Try
                Conn.Close()
            Next

            MsgBox("Asignación exitosa", MsgBoxStyle.Information)
            Me.Close()
        Else
            Me.Close()
        End If
    End Sub
End Class